package com.test.service;

import com.test.dao.BaseDao;
import com.test.dao.user.UserDao;
import com.test.dao.user.UserDaoTmpl;
import com.test.pojo.User;
import org.junit.Test;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class UserServiceTmpl implements UserService{
    // 业务层都会调用dao层，所以我们要引入dao层
    private UserDao userDao;

    public UserServiceTmpl(){
        userDao = new UserDaoTmpl();
    }

    /**
     * 根据 条件查询用户列表
     * @param userName
     * @param userRole
     * @param currentPageNo
     * @param pageSize
     * @return Object[] userList
     */
    public List<User> getUserList(String userName, int userRole, int currentPageNo, int pageSize) {
        Connection connection = null;
        connection = BaseDao.getConnection(); //获得数据库连接
        List<User> userList = null;
        try {
            userList = userDao.getUserList(connection, userName, userRole, currentPageNo,pageSize);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //关闭资源
            BaseDao.closeResource(connection,null,null);
        }
        return userList;
    }

    /**
     * 查询用户数量
     * @param userName
     * @param userRole
     * @return
     */
    public int getUserCount(String userName, int userRole) {
        Connection connection = null;
        connection = BaseDao.getConnection(); //获得数据库连接
        int userCount = 0;
        try {
            userCount = userDao.getUserCount(connection, userName, userRole);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //关闭资源
            BaseDao.closeResource(connection,null,null);
        }
        return userCount;
    }


    /**
     * 修改用户的密码
     * @param id
     * @param password
     * @return
     */
    public boolean updatePw(int id, String password) {
        Connection connection = null;
        int updateRows = 0;
        boolean flag = false;
        connection = BaseDao.getConnection(); //获得数据库连接
        try {
            updateRows = userDao.updatePwd(connection,id,password);
            if(updateRows > 0){
                flag = true;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            //关闭资源
            BaseDao.closeResource(connection,null,null);
        }

        return flag;
    }

    /**
     * 登陆鉴权
     * @param userCode 用户编码
     * @param password 用户密码
     * @return
     */
    public User login(String userCode, String password) {
        // 用户的名称和密码进行数据库匹配
        Connection connection = null; //数据库连接
        User loginUser = null;  //登陆用户
        connection = BaseDao.getConnection(); //获得数据库连接
        try {
            // 获取登陆用户信息
            loginUser = userDao.getLoginUser(connection, userCode);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            //关闭资源
            BaseDao.closeResource(connection,null,null);
        }
        return loginUser;
    }
}
